package com.cbd.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbd.pojo.Company;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.User;
import com.cbd.service.CompanyService;

@Controller
public class CompanyController {
	
	@Resource(name="companyServiceImpl")
	private CompanyService companyService;
	
	@RequestMapping(path="findCompanyById",method=RequestMethod.GET)
	public @ResponseBody Company findyCompanyById(HttpServletRequest req){
		String companyID = req.getParameter("companyID");
		System.out.println(companyID);
		Company company = companyService.findCompanyById(Integer.parseInt(companyID));
		return company;
	}
	
	@RequestMapping(path="updateCompanyInfo")
	public String updateCompanyInfo(HttpServletRequest req){
		String comName = req.getParameter("companyName");
		String passWord = req.getParameter("password");
		String companyBoss = req.getParameter("companyBoss");
		String companyTel = req.getParameter("companyTel");
		String compID= req.getParameter("id");
		Company company = new Company();
		company.setCompanyBoss(companyBoss);
		company.setCompanyTel(companyTel);
		company.setCompanyID(Integer.parseInt(compID));
		User user = new User();
		user.setUserName(comName);
		user.setUserPWD(passWord);
		company.setUser(user);
		companyService.updateCompany(company);
		return "cbd/company/welcome";
	}
	
	@RequestMapping(path="findAllEnterprise")
	public @ResponseBody PageBean findAllUserComppany(HttpServletRequest req){
		String name=req.getParameter("name");
		String page=req.getParameter("page");
		String zhi=req.getParameter("zhi");
		PageBean list=companyService.findAllUserComppany(Integer.parseInt(page), Integer.parseInt(zhi),name);
		return list;
		
	}
	@RequestMapping(path="delectEnterprise")
	public  String delectEnterprise(HttpServletRequest req){
		String id= req.getParameter("id");
		int cou=companyService.delectEnterprise(Integer.parseInt(id));
		return "cbd/company/welcome";
	}
	@RequestMapping(path="addCompany")
	public  String addCompany(Company company){
		System.out.println(company.getUser().getUserName()+" "+company.getUser().getUserPWD()+" "+company.getUser().getUserRole());
		companyService.addCompany(company);
		return "redirect:cbd/company/welcome.jsp";
	}
}
